<template>
  <div :class="className" :id="id" :style="{height:height,width:width}"></div>
</template>

<script>
  import echarts from 'echarts'
  import resize from './mixins/resize'
  import {animeChart} from '@/api/anime'

  let xData = [];
  let yData = [];

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      id: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '200px'
      },
      height: {
        type: String,
        default: '200px'
      }
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      animeChart().then((response) => {
        xData = response.data.names
        yData = response.data.values
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },

    methods: {
      initChart: function () {

        this.chart = echarts.init(document.getElementById(this.id))

        this.chart.setOption({
          title: {
            text: '番剧数-播出时间'
          },
          legend: {
            data: ['番剧数'],
            align: 'left'
          },
          tooltip: {},
          xAxis: {
            data: xData,
            silent: false,
            splitLine: {
              show: true
            }
          },
          yAxis: {},
          series: [{
            name: '番剧数',
            type: 'bar',
            data: yData,
            animationDelay: function (idx) {
              return idx * 10;
            },
            label: {
              normal: {
                show: true,
                position: 'top',
                color: 'black'
              }
            },
          }],
          animationEasing: 'elasticOut',
          animationDelayUpdate: function (idx) {
            return idx * 5;
          }
        })
      }
    }
  }
</script>
